<%@ page language="java" import="java.util.*;" pageEncoding="UTF-8"%>
<%@include file="/jsp/common/inc_default_header.jspf"%>
<html:form action="/ProjectList">
<%@include file="/jsp/common/header.jspf"%>
<!-- Start Content -->
<div id="pages">
	<h1>
		Virtium User Management
	</h1>
	<br />	
	<spam><A id="add_new_user" href="#">Add New User</A></spam>&nbsp;&nbsp;
	<spam><A id="add_new_type" href="#">Add New Type</A></spam>
	<!-- Start User Manager Dialog -->
	<table border="0" cellpadding="1" cellspacing="2" width="100%" height="100">
	<tr><td><div id="userManager_content">	
	</div></td></tr>
	<tr><td>&nbsp;</td></tr>
	<tr><td>&nbsp;</td></tr>
	<tr><td><div id="typeManager_content">	
	</div></td></tr>
	</table>
	
	<div id="add-new-user-dialog" class="inprogress">
		<div class="hd">
				User
			</div>
			<div class="bd">
				<div class="smallTitle">
					<big><b>Add New User</b> </big>
				</div>
				<br />
				<div class="smallText">
					<table border="0" cellpadding="1" cellspacing="2">
						<tr>
						<td>Full Name: </td><td><input type="text" id="txtFullName" /></td>
						</tr><tr>
						<td>User Name: </td><td><input type="text" id="txtUserName" /></td>
						</tr><tr>
						<td> Password: </td><td><input type="password" id="txtPassword" /></td>
						</tr><tr>
						<td colspan="2"> <input type="button" onClick="addNewUser();" value="Submit" /> </td>
						</tr>
					</table>
				</div>
			</div>
	</div>
	<div id="add-new-type-dialog" class="inprogress">
		<div class="hd">
				Type
			</div>
			<div class="bd">
				<div class="smallTitle">
					<big><b>Add New Type</b> </big>
				</div>
				<br />
				<div class="smallText">
					<table border="0" cellpadding="1" cellspacing="2">
						<tr>
						<td>Name: </td><td><input type="text" id="txtName" /></td>
						</tr><tr>
						<td colspan="2"> <input type="button" onClick="addNewType();" value="Submit" /> </td>
						</tr>
					</table>
				</div>
			</div>
	</div>
	<div id="content">
	</div>
	<!-- End User Manager Dialog -->

<script type="text/javascript">
var myUserDataTable,myTypeDataTable;
var content = document.getElementById("content");	
var ContextPath = "<%=request.getContextPath()%>";
YAHOO.util.Event.addListener(window, "load", function()
{
   init = function()
   {
	   	
	
		
	    // Define the callback object for Connection Manager that will set the body of our content area when the content has loaded
	    var callback =
	    {
	       success : function(o)
	       {
	       		var myUserColumnDefs = [
		         {
		            key : "id", sortable : true
		         }
		         ,
		         {
		            key : "password", sortable : true
		         }
		         ,
		         {
		            key : "fullName", sortable : true
		         }
		         ,
		         {
		            key : "userName", sortable : true
		         }
		         ,
		         {
		      	   key:'delete', label:' ', className:'delete-button'
		         }
		        ];
		        
		        var myTypeColumnDefs = [
		         {
		            key : "id", sortable : true
		         }
		         ,
		         {
		            key : "name", sortable : true
		         }
		         ,         
		         {
		      	   key:'delete', label:' ', className:'delete-button'
		         }
		        ];
	    		var response = YAHOO.lang.JSON.parse(o.responseText);
	    		var userTable = document.getElementById("userManager_content");
	    		var typeTable = document.getElementById("typeManager_content");
	    			    		
	    	    var myUserDataTableSource = new YAHOO.util.DataSource(response[0]);
	    	    myUserDataTableSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
	    	    myUserDataTableSource.responseSchema =
			    {
			       fields : ["id", "password", "fullName", "userName"]
			    };
			    
			    
	
	    	    var myPaginator1 = new YAHOO.widget.Paginator(
	    	      {
	    	         rowsPerPage : 10,
	    	         template : YAHOO.widget.Paginator.TEMPLATE_ROWS_PER_PAGE,
	    	         rowsPerPageOptions : [10, 25, 50, 100],
	    	         pageLinks : 5
	    	      }
	    	      );
	    	      
	    	   
	    	      
	    	    var myConfigs1 =
	    	      {
	    	         sortedBy :
	    	         {
	    	            key : "id", dir : "asc"
	    	         }
	    	         ,
	    	         paginator : myPaginator1,
	    	         draggableColumns : true
	    	      };
	    	    
	    	    
	    	      // Create DataTable
	    	       myUserDataTable = new YAHOO.widget.DataTable(userTable, myUserColumnDefs, myUserDataTableSource,myConfigs1);
	    	      myUserDataTable.subscribe("rowClickEvent", myUserDataTable.onEventSelectRow);
	    	      myUserDataTable.subscribe("cellDblclickEvent", myUserDataTable.onEventShowCellEditor);
	    	      myUserDataTable.subscribe("editorBlurEvent", myUserDataTable.onEventSaveCellEditor);
	
	    	      myUserDataTable.hideColumn(0);
	    	      myUserDataTable.hideColumn(1);
	    	      
	    	      myUserDataTable.subscribe('cellClickEvent',function(oArgs) {
			    	    var target = oArgs.target;
			    	    var column = this.getColumn(target);
			    	    if (column.key == 'delete') {
			    	        if (confirm('Are you sure?')) {
			    	            var record = this.getRecord(target);
			    	            var postData = "id=" + record.getData('id');
			    	            var sUrl = ContextPath + "/ProjectList.do?action=deleteUser";
			    	            YAHOO.util.Connect.asyncRequest(
			    	                    'POST',
			    	                    sUrl,
			    	                    {
			    	                        success: function (o) {
			    	                            if (o.responseText == 'Ok') {
			    	                                this.deleteRow(target);
			    	                            } else {
			    	                                alert(o.responseText);
			    	                            }
			    	                        },
			    	                        failure: function (o) {
			    	                            alert(o.statusText);
			    	                        },
			    	                        scope:this
			    	                    },
			    	                    postData
			    	                ); 
			    	        }
			    	    }
			    	});
			    	
			    	
			    	var myTypeDataTableSource = new YAHOO.util.DataSource(response[1]);
		    	    myTypeDataTableSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
		    	    myTypeDataTableSource.responseSchema =
				    {
				       fields : ["id", "name"]
				    };
			    	 var myPaginator2 = new YAHOO.widget.Paginator(
		    	      {
		    	         rowsPerPage : 10,
		    	         template : YAHOO.widget.Paginator.TEMPLATE_ROWS_PER_PAGE,
		    	         rowsPerPageOptions : [10, 25, 50, 100],
		    	         pageLinks : 5
		    	      }
		    	      );
		    	      var myConfigs2 =
		    	      {
		    	         sortedBy :
		    	         {
		    	            key : "id", dir : "asc"
		    	         }
		    	         ,
		    	         paginator : myPaginator2,
		    	         draggableColumns : true
		    	      };
			    	
			    	myTypeDataTable = new YAHOO.widget.DataTable(typeTable, myTypeColumnDefs, myTypeDataTableSource,myConfigs2);
	    	      myTypeDataTable.subscribe("rowClickEvent", myTypeDataTable.onEventSelectRow);
	    	      myTypeDataTable.subscribe("cellDblclickEvent", myTypeDataTable.onEventShowCellEditor);
	    	      myTypeDataTable.subscribe("editorBlurEvent", myTypeDataTable.onEventSaveCellEditor);
	    	      myTypeDataTable.hideColumn(0);
	    	      myTypeDataTable.subscribe('cellClickEvent',function(oArgs) {
			    	    var target = oArgs.target;
			    	    var column = this.getColumn(target);
			    	    if (column.key == 'delete') {
			    	        if (confirm('Are you sure?')) {
			    	            var record = this.getRecord(target);
			    	            var postData = "id=" + record.getData('id');
			    	            var sUrl = ContextPath + "/ProjectList.do?action=deleteType";
			    	            YAHOO.util.Connect.asyncRequest(
			    	                    'POST',
			    	                    sUrl,
			    	                    {
			    	                        success: function (o) {
			    	                            if (o.responseText == 'Ok') {
			    	                                this.deleteRow(target);
			    	                            } else {
			    	                                alert(o.responseText);
			    	                            }
			    	                        },
			    	                        failure: function (o) {
			    	                            alert(o.statusText);
			    	                        },
			    	                        scope:this
			    	                    },
			    	                    postData
			    	                ); 
			    	        }
			    	    }
			    	});
	    	      YAHOO.virtium.container.wait.hide();    	      
	    	      
	       }
	       ,
	       failure : function(o)
	       {
				YAHOO.virtium.container.wait.hide();
	       }
	    }
		var sUrl = ContextPath + "/ProjectList.do?action=getAllUserAndType";
	    var postData = "";
	    showWaitingDialog(sUrl, callback, postData, content);
	    
	    //create dialog
	    var showNewUserDlg = function(e)
	   {
		   YAHOO.util.Event.stopEvent(e);
		   AddNewUserDialog.show();
	   };
	    YAHOO.util.Dom.removeClass("add-new-user-dialog", "inprogress");
	   AddNewUserDialog = new YAHOO.widget.SimpleDialog("add-new-user-dialog",
	   {
	      width : "30em",
	      visible : false,
	      modal : true,
	      fixedcenter : true,
	      constrainToViewport : true
	   }
	   );
	   AddNewUserDialog.render();
	    YAHOO.util.Event.addListener("add_new_user", "click", showNewUserDlg , this, true);
	    
	    
	    var showNewTypeDlg = function(e)
	   {
		   YAHOO.util.Event.stopEvent(e);
		   AddNewTypeDialog.show();
	   };
	    YAHOO.util.Dom.removeClass("add-new-type-dialog", "inprogress");
	   AddNewTypeDialog = new YAHOO.widget.SimpleDialog("add-new-type-dialog",
	   {
	      width : "30em",
	      visible : false,
	      modal : true,
	      fixedcenter : true,
	      constrainToViewport : true
	   }
	   );
	   AddNewTypeDialog.render();
	    YAHOO.util.Event.addListener("add_new_type", "click", showNewTypeDlg , this, true);
   }();
});

function addNewUser(){
	var fullName = document.getElementById("txtFullName").value;
	var userName = document.getElementById("txtUserName").value;
	var password = document.getElementById("txtPassword").value;
	var postData = "fullName="+fullName+"&userName=" + userName + "&password=" + password;
	AddNewUserDialog.hide();
	var callback =
	    {
	       success : function(o)
	       {
	    		 var value = o.responseText;
	    		 //fields : ["id", "password", "fullName", "userName"]
		         myUserDataTable.addRow(
		         {
		            "id" : value.substring(0,value.indexOf(":")),
		            "password" : value.substring(value.indexOf(":")+1,value.length),
		            "fullName" : fullName,
		            "userName" : userName
		         }
		         , 0);
		         
	    		YAHOO.virtium.container.wait.hide();    	      
	    	      
	       }
	       ,
	       failure : function(o)
	       {
				YAHOO.virtium.container.wait.hide();
	       }
	    }
		var sUrl = ContextPath + "/ProjectList.do?action=addNewUser";
	    
	    showWaitingDialog(sUrl, callback, postData, content);
}

function addNewType(){
	var name = document.getElementById("txtName").value;	
	var postData = "name="+name;
	AddNewTypeDialog.hide();
	var callback =
	    {
	       success : function(o)
	       {
	    		 var value = o.responseText;
	    		 //fields : ["id", "password", "fullName", "userName"]
		         myTypeDataTable.addRow(
		         {
		            "id" : value,
		            "name" : name
		         }
		         , 0);
		         
	    		YAHOO.virtium.container.wait.hide();    	      
	    	      
	       }
	       ,
	       failure : function(o)
	       {
				YAHOO.virtium.container.wait.hide();
	       }
	    }
		var sUrl = ContextPath + "/ProjectList.do?action=addType";
	    
	    showWaitingDialog(sUrl, callback, postData, content);
}
</script>
<br/><br/><br/><br/>
<table>
	<tr>
	<td>&nbsp;</td>
	</tr><tr>
	<td>&nbsp;</td>
	</tr><tr>
	<td>&nbsp;</td>
	</tr><tr>
	<td>&nbsp;</td>
	</tr>
</table>
<br/><br/><br/>
</html:form>
</div>

<!-- End Content -->
<%@include file="/jsp/common/footer.jspf"%>